package cn.cxj.org.controller;

import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.LogAnnotation;
import cn.cxj.basic.util.PageList;
import cn.cxj.org.domain.Department;
import cn.cxj.org.query.DepartmentQuery;
import cn.cxj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门接口
 * @author 陈鑫杰
 */
@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增或者修改 POST
     * @param department
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @LogAnnotation(operateType = "新增或者修改方法")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id删除 DELETE
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(operateType = "根据id删除")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 基于id查询 GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @LogAnnotation(operateType = "基于id查询")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询全部 GET
     * @return
     */
    @GetMapping("/list")
    @LogAnnotation(operateType = "查询全部")
    public List<Department> findAll(){
        return departmentService.queryAll();
    }


    /**
     * 分页和高级查询 POST
     * @param query
     * @return
     */
    @PostMapping("/page")
    @LogAnnotation(operateType = "分页和高级查询")
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @LogAnnotation(operateType = "批量删除")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    /**
     * 查询部门树 一级和二级
     * @return
     */
    @PostMapping("/tree")
    @LogAnnotation(operateType = "查询部门树")
    public List<Department> findTree(){
        return departmentService.findOneAndTwoLevelTree();
    }
}
